package com.edu.chat.web.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.chat.web.common.enums.ResultCodeEnum;
import com.edu.chat.web.common.model.BizException;
import com.edu.chat.web.common.model.R;
import com.edu.chat.web.controller.vo.ArticleVO;
import com.edu.chat.web.covert.ArticleCovert;
import com.edu.chat.web.dto.ArticleDTO;
import com.edu.chat.web.integration.MilvusClient;
import com.edu.chat.web.mapper.CsArticleMapper;
import com.edu.chat.web.model.CsArticle;
import com.edu.chat.web.service.CsArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
* @author 86177
* @description 针对表【cs_article(QA知识项信息表)】的数据库操作Service实现
* @createDate 2025-02-22 15:02:27
*/
@Service
@Slf4j
public class CsArticleServiceImpl extends ServiceImpl<CsArticleMapper, CsArticle>
    implements CsArticleService{

	@Autowired
	private MilvusClient milvusClient;

	@Override
	public List<ArticleDTO> listArticleByCategoryId(String categoryId, String libraryId) {
		if (StringUtils.isBlank(categoryId) || StringUtils.isBlank(libraryId)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID, "CsArticleServiceImpl categoryId or libraryId is blank");
		}

		LambdaQueryWrapper<CsArticle> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CsArticle::getCategoryId, categoryId)
				.eq(CsArticle::getLibraryId, libraryId)
				.eq(CsArticle::getDeleted, 0);

		List<CsArticle> list = list(queryWrapper);

		if(CollectionUtil.isEmpty(list)){
			log.info("CsArticleServiceImpl listArticleByCategoryId list is empty");
			return List.of();
		}

		return list.stream().map(ArticleCovert::covert).toList();
	}

	@Override
	public R<Boolean> createArticle(ArticleVO articleVO) {

		if (articleVO == null){
			throw new BizException(ResultCodeEnum.PARAM_INVALID, "CsArticleServiceImpl articleVO is null");
		}

		articleVO.setId(IdUtil.getSnowflakeNextIdStr());

		CsArticle covert = ArticleCovert.covert(articleVO);

		if (save(covert)){
			milvusClient.insertData(Collections.singletonList(ArticleCovert.covertToArticleEntity(articleVO)));
			return R.success(Boolean.TRUE);
		}

		return R.fail(ResultCodeEnum.DATABASE_ERROR);

	}

	@Override
	public R<Boolean> updateArticle(ArticleVO articleVO) {

		if (articleVO == null){
			throw new BizException(ResultCodeEnum.PARAM_INVALID, "CsArticleServiceImpl articleVO is null");
		}

		if (updateById(ArticleCovert.covert(articleVO))){
			return R.success(Boolean.TRUE);
		}

		return R.fail(ResultCodeEnum.FAIL);
	}

	@Override
	public R<Boolean> deleteArticle(String id) {

		log.info("CsArticleServiceImpl deleteArticle id:{}", id);

		if (StringUtils.isBlank(id)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID, "CsArticleServiceImpl id is blank");
		}

		if (removeById(id)){
			return R.success(Boolean.TRUE);
		}
		return R.fail(ResultCodeEnum.FAIL);
	}

	@Override
	public R<ArticleDTO> getArticleById(String id) {
		log.info("CsArticleServiceImpl getArticleById id:{}", id);
		if (StringUtils.isBlank(id)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID, "CsArticleServiceImpl id is blank");
		}

		CsArticle byId = getById(id);
		if ( byId!= null){
			return R.success(ArticleCovert.covert(byId));
		}

		return R.fail(ResultCodeEnum.FAIL);
	}
}




